package spring.angularjs.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import spring.angularjs.domain.Userinfo;
import spring.angularjs.domain.json.UserTemp;
import spring.angularjs.services.Services;

@Controller
@RequestMapping(value="/updateinfo")
public class UpdateInfomationController {
	
	@Autowired
	private Services services;
	
	private byte[] imageTemp = null;
	
	@RequestMapping(value="/start")
	public String goUpdatePage(){
		HomeController.page = "update";
		return "detail/updateinformation";
	}
	
	// not using headers=application/json
	@RequestMapping(value="/getlist", method=RequestMethod.GET)
	@ResponseBody
	public List<UserTemp> getListUser(){
		List<Userinfo> list = services.userNomal();
		List<UserTemp> list_temp = new ArrayList<UserTemp>();
		for (int i = 0; i < list.size(); i++) {
			Userinfo user = list.get(i);
			UserTemp userTemp = new UserTemp();
			if(user.getImage() != null){
				userTemp = new UserTemp(user.getLoginId(), user.getStatus()
						.getStatusid(), user.getRole().getRoleid(), user.getPassword(),
						user.getCardnumberid(), user.getFirstname(),
						user.getLastname(), user.getMidname(), user.getPhone1(),
						user.getPhone2(), user.getAddress1(), user.getAddress2(),
						user.getEmail1(), user.getEmail2(), new String(user.getImage()));
			} else {
				userTemp = new UserTemp(user.getLoginId(), user.getStatus()
						.getStatusid(), user.getRole().getRoleid(), user.getPassword(),
						user.getCardnumberid(), user.getFirstname(),
						user.getLastname(), user.getMidname(), user.getPhone1(),
						user.getPhone2(), user.getAddress1(), user.getAddress2(),
						user.getEmail1(), user.getEmail2());
			}
			list_temp.add(userTemp);
		}
		return list_temp;
	}
	
	@RequestMapping(value="/get/{id}", method=RequestMethod.POST)
	@ResponseBody
	public UserTemp getUserInfo(@PathVariable("id") String id){
		Userinfo user = services.getUserInfo(id);
		UserTemp userTemp = new UserTemp();
		if(user.getImage() != null){
			userTemp = new UserTemp(user.getLoginId(), user.getStatus()
					.getStatusid(), user.getRole().getRoleid(), user.getPassword(),
					user.getCardnumberid(), user.getFirstname(),
					user.getLastname(), user.getMidname(), user.getPhone1(),
					user.getPhone2(), user.getAddress1(), user.getAddress2(),
					user.getEmail1(), user.getEmail2(), new String(user.getImage()));
		} else {
			userTemp = new UserTemp(user.getLoginId(), user.getStatus()
					.getStatusid(), user.getRole().getRoleid(), user.getPassword(),
					user.getCardnumberid(), user.getFirstname(),
					user.getLastname(), user.getMidname(), user.getPhone1(),
					user.getPhone2(), user.getAddress1(), user.getAddress2(),
					user.getEmail1(), user.getEmail2());
		}
		return userTemp;
	}
	
	@RequestMapping(value = "/file/upload", method = RequestMethod.POST)
	@ResponseBody
	public void fileUpload() {
	}
	
	@RequestMapping(value = "/file/image", method = RequestMethod.POST)
	@ResponseBody
	public byte[] createImageUpload(@RequestBody byte[] data) {
		imageTemp = data;
		return data;
	}
	
	@RequestMapping(value="/passwordold/updateuser", method=RequestMethod.POST)
	@ResponseBody
	public void updateUserNotChangePassword(@RequestBody UserTemp userTemp){
		HomeController.page = "update";
		Userinfo user = services.getUserInfo(userTemp.getLoginId());
		user.setCardnumberid(userTemp.getCardnumberid());
		user.setFirstname(userTemp.getFirstname());
		user.setLastname(userTemp.getLastname());
		user.setMidname(userTemp.getMidname());
		user.setPhone1(userTemp.getPhone1());
		user.setPhone2(userTemp.getPhone2());
		user.setAddress1(userTemp.getAddress1());
		user.setAddress2(userTemp.getAddress2());
		user.setEmail1(userTemp.getEmail1());
		user.setEmail2(userTemp.getEmail2());
		if(imageTemp != null){
			user.setImage(imageTemp);
		}
		
		services.update(user);
		
		imageTemp = null;
	}
	
	@RequestMapping(value="/passwordnew/updateuser", method=RequestMethod.POST)
	@ResponseBody
	public void updateUserChangePassword(@RequestBody UserTemp userTemp){
		HomeController.page = "update";
		BCryptPasswordEncoder encode = new BCryptPasswordEncoder();
		String newpassword = encode.encode(userTemp.getPassword());
		
		Userinfo user = services.getUserInfo(userTemp.getLoginId());
		user.setPassword(newpassword);
		user.setCardnumberid(userTemp.getCardnumberid());
		user.setFirstname(userTemp.getFirstname());
		user.setLastname(userTemp.getLastname());
		user.setMidname(userTemp.getMidname());
		user.setPhone1(userTemp.getPhone1());
		user.setPhone2(userTemp.getPhone2());
		user.setAddress1(userTemp.getAddress1());
		user.setAddress2(userTemp.getAddress2());
		user.setEmail1(userTemp.getEmail1());
		user.setEmail2(userTemp.getEmail2());
		if(imageTemp != null){
			user.setImage(imageTemp);
		}
		
		services.update(user);
		
		imageTemp = null;
	}
	

}
